//获取应用实例
const app = getApp();

//声明全局变量

let proListToTop = [],
  menuToTop = [],
  MENU = 0,
  windowHeight, timeoutId;
// MENU ==> 是否为点击左侧进行滚动的，如果是，则不需要再次设置左侧的激活状态
Page({

  data: {
    staticImg: app.globalData.staticImg,
    currentActiveIndex: 0,
    // 接口返回的商品数组
    navList: [],
    animate: '',
    goodsList: [],
    page: 1,
    searchName: '',
    titleName: '',
  },
  onLoad: function(e) {
    // 确保页面数据已经刷新完毕~
    setTimeout(() => {
      // this.getAllRects()
    }, 20)
    console.log(e.index)
    if (e.index == 1) {
      this.setData({
        titleName: '新人0元购'
      })
    } else if (e.index == 2) {
      this.setData({
        titleName: '今日疯抢'
      })
    } else if (e.index == 3) {
      this.setData({
        titleName: '聚划算榜单'
      })
    } else if (e.index == 4) {
      this.setData({
        titleName: '超级券'
      })
    } else if (e.index == 5) {
      this.setData({
        titleName: '9.9包邮'
      })
    } else if (e.index == 6) {
      this.setData({
        titleName: '必买清单'
      })
    }
    wx.setNavigationBarTitle({
      title: this.data.titleName
    })
    this.setData({
      searchName: e.id
    })
    this.getAllList(e.index);
    wx.getStorage({
      key: 'token',
      success: function(res) {
        console.log(res)
        if (res.data != '') {
          app.globalData.token = res.data
        }
      },
    })
  },

  //获取9块9包邮商品列表
  getAllList(index) {
    var that = this;
    var url;
    var eliteId;
    var data;
    if (index == 1) {
      url = app.globalData.api + '/app.php?c=Tbk&a=getFreeGoodsList';
      data = {
        token: app.globalData.token,
        p:1,
        per:10
      }
    } else if (index == 2) {

    } else if (index == 3) {
      url = app.globalData.api + '/app.php?c=Haodanku&a=getSalesList'; //运营频道商品查询-推荐商品
      data = {
        token: app.globalData.token,
        sale_type: 2
      }
    } else if (index == 4) {
      url = app.globalData.api + '/app.php?c=Jingdong&a=getRecommendGoods'; //京仓配送
      data = {
        eliteId: 15
      }
    } else if (index == 5) {
      url = app.globalData.api + '/app.php?c=Tbk&a=get99List';
      data = {
        token: app.globalData.token,
      }
    } else if (index == 6) {
      url = app.globalData.api + '/app.php?c=Jingdong&a=getRecommendGoods'; //超级大卖场
      data = {
        eliteId: 2
      }
    }
    wx.request({
      url: url,
      data: {
        data
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        console.log(res);
        that.setData({
          navList: that.data.navList.concat(res.data.data.list)
        })
      },
      fail: function() {

      }
    });
  },

  goodDetail: function(e) {
    console.log(app.globalData.token)
    if (app.globalData.token != '') {
      console.log(e);
      wx.navigateTo({
        url: 'item?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.reLaunch({
        url: '/pages/mine/login',
      })
    }

  },

  scroll(e) {
    // console.log(e);
    for (let i = 0; i < proListToTop.length; i++) {
      if (e.detail.scrollTop < proListToTop[i] && i !== 0 && e.detail.scrollTop > proListToTop[i - 1]) {
        return this.setDis(i)
      }
    }
    // 找不到匹配项，默认显示第一个数据
    if (!MENU) {
      this.setData({
        currentActiveIndex: 0
      })
    }
    MENU = 0
  },

  getActiveReacts() {
    wx.createSelectorQuery().selectAll('.menu-active').boundingClientRect(function(rects) {
      return rects[0].top
    }).exec()
  },
  getAllRects() {
    wx.createSelectorQuery().selectAll('.pro-item').boundingClientRect(function(rects) {
      rects.forEach(function(rect) {
        console.log(rect)
        // 这里减去44是根据你的滚动区域距离头部的高度，如果没有高度，可以将其删去
        proListToTop.push(rect.top - 44)
      })
    }).exec()
    // 获取menu数组的位置信息
    wx.createSelectorQuery().selectAll('.menu-item').boundingClientRect(function(rects) {
      wx.getSystemInfo({
        success: function(res) {
          // console.log(res);
          windowHeight = res.windowHeight / 2
          // console.log(windowHeight)
          rects.forEach(function(rect) {
            menuToTop.push({
              top: rect.top,
              // animate: rect.top > windowHeight
            })
          })
        }
      })
    }).exec()
  },
  // 获取系统的高度信息
  getSystemInfo() {
    let self = this
    wx.getSystemInfo({
      success: function(res) {
        windowHeight = res.windowHeight / 2
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading(); //在标题栏中显示加载图标
    setTimeout(function() {
      wx.stopPullDownRefresh(); //停止加载
      wx.hideNavigationBarLoading(); //隐藏加载icon
    }, 2000)
    this.setData({
      page: 1,
      navList: []
    })
    that.getAllList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // this.getCartList()
    var that = this;
    that.setData({
      page: that.data.page + 1
    })
    that.getAllList();
  },


})